package exabase.localClientProxy;

import static org.junit.Assert.assertEquals;

import java.io.IOException;
import java.util.logging.Logger;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import exabase.Exabase;
import exabase.data.DataStore;
import exabase.data.Hash;
import exabase.meta.MetaStore;

public class LocalClientProxyTest {
	private static Logger logger = Logger.getLogger(LocalClientProxyTest.class
			.getName());

	private String data1 = "testFiles/data1.txt";
	private Hash hash1 = new Hash("6438f31616e9aba886a4c4820a4aaba4");
	private String meta1 = "testFiles/meta1.xml";
	private String meta2 = "testFiles/meta2.xml";

	@Before
	public void setUp() {
		Exabase.getInstance().go();
		DataStore.reset();
		MetaStore.reset();
	}

	@After
	public void after() throws IOException, Exception {
		LocalClientProxy.getInstance().disconnect();
	}

	@Test
	public void testAdd() throws IOException, Exception {
		Hash hash = LocalClientProxy.getInstance().add(data1, meta1);
		Thread.sleep(2);

		assertEquals("the hash should be correct", hash1, hash);
		assertEquals("there should be one data", 1, DataStore.getInstance()
				.getDataSet().size());
		assertEquals("the hash should be correct", hash1, DataStore
				.getInstance().getDataSet().iterator().next().getHash());
		assertEquals("there should be one meta", 1, MetaStore.getInstance()
				.getMetaSet().size());
		assertEquals("the meta fileURL should be correct", meta1, MetaStore
				.getInstance().getMetaSet().iterator().next().getMetaFileName());
	}

	@Test
	public void testGetAllMeta() throws IOException, ClassNotFoundException,
			Exception {
		LocalClientProxy.getInstance().add(data1, meta1);
		LocalClientProxy.getInstance().getAllMeta();
		Thread.sleep(2);
		assertEquals("there should be one meta", 1, LocalClientProxy
				.getInstance().metas.size());
		assertEquals("the meta fileURL should be correct", meta1,
				LocalClientProxy.getInstance().metas.iterator().next()
						.getMetaFileName());
	}

	@Test
	public void testGetData() throws IOException, Exception {
		logger.finer("adding data");
		LocalClientProxy.getInstance().add(data1, meta1);
		logger.finer("get data");
		LocalClientProxy.getInstance().getData(hash1);
		Thread.sleep(2);
		logger.finer("do asserts");
		assertEquals("there should be one data", 1, LocalClientProxy
				.getInstance().getDataSet().size());
		assertEquals("the hash should be correct", hash1, LocalClientProxy
				.getInstance().getDataSet().iterator().next().getHash());
	}

	@Test
	public void testUpdateMeta() throws IOException, Exception {
		LocalClientProxy.getInstance().add(data1, meta1);
		LocalClientProxy.getInstance().updateMeta(hash1, meta2);
		Thread.sleep(2);
		assertEquals("there should be one meta", 1, MetaStore.getInstance()
				.getMetaSet().size());
		assertEquals("the meta fileURL should be correct", meta2, MetaStore
				.getInstance().getMetaSet().iterator().next().getMetaFileName());
	}
}
